package cn.wolfcode.car.controller;

import cn.wolfcode.car.domain.Appointment;
import cn.wolfcode.car.domain.Business;
import cn.wolfcode.car.domain.SystemDictionaryItem;
import cn.wolfcode.car.enumpkg.AppointmentStatusEnum;
import cn.wolfcode.car.exception.AppointmentException;
import cn.wolfcode.car.qo.AppointmentQueryObject;
import cn.wolfcode.car.service.IAppointmentService;
import cn.wolfcode.car.service.IBusinessService;
import cn.wolfcode.car.service.ISystemDictionaryItemService;
import cn.wolfcode.car.service.ISystemDictionaryService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import result.JsonResult;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

@Controller
@RequestMapping("/appointment")
public class AppointmentController {
    @Autowired
    private IAppointmentService appointmentService;
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;
    @Autowired
    private IBusinessService businessService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") AppointmentQueryObject qo) {
        PageInfo<Appointment> list = appointmentService.list(qo);
        System.out.println("---------------" + list.getList());
        model.addAttribute("result", list);
        List<Appointment> appointments = appointmentService.listAll();
        model.addAttribute("appointments", appointments);
        //业务大类
        List<SystemDictionaryItem> category = systemDictionaryItemService.getByType(1L);
        model.addAttribute("category", category);
        //门店
        List<Business> businesses = businessService.listAll();
        model.addAttribute("businesses", businesses);
        //状态
        AppointmentStatusEnum[] status = AppointmentStatusEnum.values();
        model.addAttribute("status", status);

        return "appointment/list";
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Appointment appointment, HttpServletRequest request) throws ParseException {
        System.out.println("appointment============" + appointment);
        if (appointment.getId() == null) {
            //判断当前手机号预约过多少次,如果超过三次不能预约,提示预约次数过多
            //在application中存放map(telMap)一个手机号和预约次数
            HashMap<String, Integer> telMap = (HashMap<String, Integer>) request.getServletContext().getAttribute("telMap");
            if (telMap == null) {
                telMap = new HashMap<>();
                telMap.put(appointment.getContactTel(), 1);
                request.getServletContext().setAttribute("telMap", telMap);
            } else {
                if (telMap.containsKey(appointment.getContactTel())) {
                    //里面有,判断是否超过三
                    int times;
                    if ((times = telMap.get(appointment.getContactTel())) == 3) {
                        //预约达到三次
                        throw new AppointmentException("预约次数已达三次,不能预约");
                    } else {
                        telMap.put(appointment.getContactTel(), times + 1);
                        request.getServletContext().setAttribute("telMap", telMap);
                    }
                } else {
                    telMap = new HashMap<>();
                    telMap.put(appointment.getContactTel(), 1);
                    request.getServletContext().setAttribute("telMap", telMap);
                }
            }
            //新增

            //随机数
            Date date22 = new Date();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
            String d = formatter.format(date22);
            Random r = new Random(1);
            int ran1 = r.nextInt(100000);
            String ano = d + ran1;
            appointment.setAno(ano);

            appointment.setCreateTime(new Date());
            //状态
            appointment.setStatus(0);
            System.out.println("----------------------------------" + appointment);
            appointmentService.save(appointment);
        } else {
            appointmentService.update(appointment);
        }
        return "redirect:/appointment/list";
    }

    @PostMapping("/statusChange")
    @ResponseBody
    public JsonResult statusChange(Appointment appointment) {
        appointmentService.update(appointment);
        return JsonResult.success(null);
    }

    @GetMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        appointmentService.delete(id);
        return JsonResult.success(null);
    }
}
